home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.2 / Bullet / strikefont.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  8.2 KB  |  332 lines

  1. #define  DEBUG
  2. #ifdef   DEBUG
  3. #define  D(a)    kprintf a
  4. #else
  5. #define  D(a)
  6. #endif
  7. #include <exec/types.h>
  8. #include <dos/rdargs.h>
  9. #include <graphics/rastport.h>
  10. #include <intuition/intuition.h>
  11. #include "libraries/diskfonttag.h"
  12. #include "libraries/glyph.h"
  13.  
  14. #include <clib/exec_protos.h>
  15. #include <clib/dos_protos.h>
  16. #include <clib/graphics_protos.h>
  17. #include <clib/intuition_protos.h>
  18. #include <clib/utility_protos.h>
  19. #include "clib/bullet_protos.h"
  20. #include <pragmas/exec_pragmas.h>
  21. #include <pragmas/dos_pragmas.h>
  22. #include <pragmas/graphics_pragmas.h>
  23. #include <pragmas/intuition_pragmas.h>
  24. #include <pragmas/utility_pragmas.h>
  25. #include "pragmas/bullet_pragmas.h"
  26.  
  27. #include <math.h>
  28. #include <string.h>
  29. #undef    strchr
  30. #undef    strcmp
  31. #undef    strcpy
  32. #undef    strrchr
  33. #undef    memset
  34.  
  35. extern struct Library *SysBase;
  36. extern struct Library *DOSBase;
  37.  
  38. #define  TEMPLATE    "NAME,DPIX/N,DPIY/N,DOTPX/N,DOTPY/N"
  39.  
  40. #define  O_NAME        0
  41. #define  O_DPIX        1
  42. #define  O_DPIY        2
  43. #define  O_DOTPX    3
  44. #define  O_DOTPY    4
  45. #define  O_COUNT    5
  46.  
  47. #define  U_DPI        0
  48. #define  U_DOTP        1
  49. #define  U_COUNT    2
  50.  
  51. #define  MAXFILESIZE    4000
  52.  
  53. struct Library *GfxBase = 0;
  54. struct Library *IntuitionBase = 0;
  55. struct Library *UtilityBase = 0;
  56. struct RDArgs *RDArgs = 0;
  57. BPTR OTFile = 0;
  58. struct Library *BulletBase = 0;
  59. struct GlyphEngine *GlyphEngine = 0;
  60. struct TagItem OTags[MAXFILESIZE/sizeof(struct TagItem)];
  61. struct TagItem UTags[U_COUNT+1] = {
  62.     { OT_DeviceDPI, 0x00480048 },
  63.     { OT_DotSize, 0x00640064 },
  64.     { TAG_DONE, 0 }
  65. };
  66.  
  67. #define  X2    640
  68. #define  Y2    400
  69. #define  CX    (X2/2)
  70. #define  CY    (Y2/2)
  71. struct Window *Window = 0;
  72. struct IntuiMessage *IM;
  73. PLANEPTR Template = 0;
  74.  
  75. void
  76. endGame(format, arg1, arg2, arg3, arg4)
  77. char *format, *arg1, *arg2, *arg3, *arg4;
  78. {
  79.     if (format) {
  80.     D((format, arg1, arg2, arg3, arg4));
  81.     printf(format, arg1, arg2, arg3, arg4);
  82.     }
  83.     if (Template)
  84.     FreeRaster(Template, X2, Y2);
  85.     if (Window)
  86.     CloseWindow(Window);
  87.     if (GlyphEngine)
  88.     CloseEngine(GlyphEngine);
  89.     if (BulletBase)
  90.     CloseLibrary(BulletBase);
  91.     if (OTFile)
  92.     Close(OTFile);
  93.     if (RDArgs)
  94.     FreeArgs(RDArgs);
  95.     if (UtilityBase)
  96.     CloseLibrary(UtilityBase);
  97.     if (IntuitionBase)
  98.     CloseLibrary(IntuitionBase);
  99.     if (GfxBase)
  100.     CloseLibrary(GfxBase);
  101.     if (format)
  102.     exit(5);
  103.     exit(0);
  104. }
  105.  
  106.  
  107. struct Window *
  108. myOpenWindowTags(tag)
  109. LONG tag;
  110. {
  111.     return(OpenWindowTagList(0, (struct TagItem *) &tag));
  112. }
  113.  
  114.  
  115. ULONG
  116. setInfo(ge, tag)
  117. struct GlyphEngine *ge;
  118. LONG tag;
  119. {
  120.     return(SetInfoA(ge, (struct TagItem *) &tag));
  121. }
  122.  
  123. ULONG
  124. obtainInfo(ge, tag)
  125. struct GlyphEngine *ge;
  126. LONG tag;
  127. {
  128.     return(ObtainInfoA(ge, (struct TagItem *) &tag));
  129. }
  130.  
  131. ULONG
  132. releaseInfo(ge, tag)
  133. struct GlyphEngine *ge;
  134. LONG tag;
  135. {
  136.     return(ReleaseInfoA(ge, (struct TagItem *) &tag));
  137. }
  138.  
  139. void
  140. main()
  141. {
  142.     struct GlyphMap *glyph;
  143.     ULONG error, *options[O_COUNT], result;
  144.     int i, x, y, dx, dy;
  145.     UWORD chId;
  146.     char pathStore[256], *filePath, *s;
  147.  
  148.     D(("Test\nOpenLibrarys..."));
  149.     GfxBase = OpenLibrary("graphics.library", 0);
  150.     if (!GfxBase)
  151.     endGame("ERROR: cannot open \"graphics.library\"\n");
  152.     IntuitionBase = OpenLibrary("intuition.library", 0);
  153.     if (!IntuitionBase)
  154.     endGame("ERROR: cannot open \"intuition.library\"\n");
  155.     UtilityBase = OpenLibrary("utility.library", 0);
  156.     if (!UtilityBase)
  157.     endGame("ERROR: cannot open \"utility.library\"\n");
  158.  
  159.     D(("ReadArgs..."));
  160.     memset(options, 0, sizeof(options));
  161.     RDArgs = ReadArgs(TEMPLATE, (LONG *) options, 0);
  162.     if (!RDArgs)
  163.     endGame("ERROR: invalid arguments\n");
  164.  
  165.     D((" .\n"));
  166.     /* generate .otag file path */
  167.     strcpy(pathStore, "FONTS:");
  168.     filePath = pathStore+6;
  169.     if (options[0])
  170.     strcpy(filePath, (char *) options[O_NAME]);
  171.     else
  172.     strcpy(filePath, "CGTimes.font");
  173.     s = strrchr(filePath, '.');
  174.     if ((s == 0) || (strcmp(s, ".font")))
  175.     endGame("ERROR: NAME not .font name\n");
  176.  
  177.     strcpy(s, OTSUFFIX);
  178.  
  179.     D((".otag file \"%s\"\n", filePath));
  180.     /* open .otag file */
  181.     OTFile = Open(filePath, MODE_OLDFILE);
  182.     if (!OTFile) {
  183.     if (!strchr(filePath, ':')) {
  184.         filePath = pathStore;
  185.         OTFile = Open(filePath, MODE_OLDFILE);
  186.     }
  187.     }
  188.  
  189.     if (!OTFile)
  190.     endGame("no font file \"%s\"\n", filePath);
  191.  
  192.     strcpy(s, ".font");
  193.     D((".font file \"%s\"\n", filePath));
  194.  
  195.     /* read and verify the .otag */
  196.     if (Read(OTFile, OTags, sizeof(struct TagItem)) != sizeof(struct TagItem))
  197.     endGame("OTFile read fail (8 bytes)\n");
  198.  
  199.     if (OTags[0].ti_Tag != OT_FileIdent)
  200.     endGame(".otag first tag $%lx, not $%lx\n", OTags[0].ti_Tag,
  201.         OT_FileIdent);
  202.  
  203.     Seek(OTFile, 0, OFFSET_END);
  204.     if (Seek(OTFile, 0, OFFSET_BEGINNING) != OTags[0].ti_Data)
  205.     endGame(".otag file size wrong\n");
  206.  
  207.     if (OTags[0].ti_Data > MAXFILESIZE)
  208.     endGame(".otag file size %ld larger than program maximum %ld\n",
  209.         OTags[0].ti_Data, MAXFILESIZE);
  210.  
  211.     /* this is a valid .otag file header, read it */
  212.     if (Read(OTFile, OTags, OTags[0].ti_Data) != OTags[0].ti_Data)
  213.     endGame(".otag read failure\n");
  214.  
  215.     /* patch indirect pointers */
  216.     for (i = 0; i < OTags[0].ti_Data/sizeof(struct TagItem); i++) {
  217.     if (OTags[i].ti_Tag == TAG_DONE)
  218.         break;
  219.     if (OTags[i].ti_Tag & OT_Indirect)
  220.         OTags[i].ti_Data += (ULONG) OTags;
  221.     }
  222.  
  223.     /* ensure the associated glyph library is open */
  224.     /* get the library name */
  225.     s = (char *) GetTagData(OT_Engine, 0, OTags);
  226.     if (!s)
  227.     endGame("no OT_Engine tag\n");
  228.  
  229.     if (strcmp(s, "bullet"))
  230.     endGame("OT_Engine not \"bullet\" but \"%s\"\n", s);
  231.  
  232.     BulletBase = OpenLibrary("bullet.library", 0);
  233.     if (!BulletBase)
  234.     endGame("OpenLibrary \"bullet.library\" failed\n");
  235.  
  236.     GlyphEngine = OpenEngine();
  237.  
  238.     if (!GlyphEngine)
  239.     endGame("OpenEngine failed\n");
  240.  
  241.     error = setInfo(GlyphEngine, OT_OTagPath, filePath, TAG_DONE);
  242.     if (error)
  243.     endGame("setInfo(OTagPath) error %ld\n", error);
  244.  
  245.     error = setInfo(GlyphEngine, OT_OTagList, OTags, TAG_DONE);
  246.     if (error)
  247.     endGame("setInfo(OTagList) error %ld\n", error);
  248.  
  249.     /* modify environment from .otag */
  250.     if (result = GetTagData(OT_DeviceDPI, 0, OTags))
  251.     UTags[U_DPI].ti_Data = result;
  252.     if (result = GetTagData(OT_DotSize, 0, OTags))
  253.     UTags[U_DOTP].ti_Data = result;
  254.  
  255.     /* modify environment from parameters */
  256.     if (options[O_DPIX] && options[O_DPIY])
  257.     UTags[U_DPI].ti_Data = (*options[O_DPIX]<<16) | *options[O_DPIY];
  258.     if (options[O_DOTPX] && options[O_DOTPY])
  259.     UTags[U_DOTP].ti_Data = (*options[O_DOTPX]<<16) | *options[O_DOTPY];
  260.  
  261.     if (error = SetInfoA(GlyphEngine, UTags))
  262.     endGame("setInfo(UTags) error %ld\n", error);
  263.     if (error = setInfo(GlyphEngine, OT_PointHeight, 0x00140000, TAG_DONE))
  264.     endGame("setInfo(20 points) failed %ld\n", error);
  265.  
  266.     Window = myOpenWindowTags(
  267.         WA_Left, 0,
  268.         WA_Top, 0,
  269.         WA_Width, X2,
  270.         WA_Height, Y2,
  271.         WA_IDCMP, CLOSEWINDOW,
  272.         WA_CloseGadget, TRUE,
  273.         WA_Title, "Strikefont",
  274.         TAG_DONE);
  275.     
  276.     if (!Window)
  277.     endGame("OpenWindow failed\n");
  278.  
  279.     Template = AllocRaster(X2, Y2);
  280.     if (!Template)
  281.     endGame("AllocRaster failed\n");
  282.  
  283.     x = 4;
  284.     y = 30;
  285.     chId = 0;
  286.     SetAPen(Window->RPort, 1);
  287.     SetDrMd(Window->RPort, JAM1);
  288.     do {
  289.     do {
  290.         if (chId == 0x3000)
  291.         chId = 0xe800;
  292.         if (error = setInfo(GlyphEngine, OT_GlyphCode, chId++, TAG_DONE))
  293.         endGame("setInfo() failed %ld\n", error);
  294.         if (!obtainInfo(GlyphEngine, OT_GlyphMap, &glyph, TAG_DONE)) {
  295.         for (i = (glyph->glm_BMRows*glyph->glm_BMModulo/4) - 1;
  296.             i >= 0; i--)
  297.             ((ULONG *) Template)[i] = ((ULONG *) glyph->glm_BitMap)[i];
  298.         dx = x - glyph->glm_X0 + glyph->glm_BlackLeft;
  299.         dy = y - glyph->glm_Y0 + glyph->glm_BlackTop;
  300.         BltTemplate(((char *) Template)+
  301.             (glyph->glm_BlackTop*glyph->glm_BMModulo)+
  302.             ((glyph->glm_BlackLeft/16)*2), glyph->glm_BlackLeft&0xf,
  303.             glyph->glm_BMModulo, Window->RPort, dx, dy,
  304.             glyph->glm_BlackWidth, glyph->glm_BlackHeight);
  305.         releaseInfo(GlyphEngine, OT_GlyphMap, glyph, TAG_DONE);
  306.         x += glyph->glm_X1-glyph->glm_X0;
  307.         }
  308.     }
  309.         while (x < 620);
  310.     x = 4;
  311.     y += 20;
  312.     }
  313.     while (y < 390);
  314.  
  315.     D(("done w/ strikefont.\n"));
  316.     D(("UserPort $%lx\n", Window->UserPort));
  317.     /* wait until done before returning */
  318.     for (;;) {
  319.     while (IM = (struct IntuiMessage *) GetMsg(Window->UserPort)) {
  320.         D(("IM Class $%08lx\n", IM->Class));
  321.         if (IM->Class & CLOSEWINDOW) {
  322.         D(("CLOSEWINDOW\n"));
  323.         endGame(0);
  324.         }
  325.         ReplyMsg((struct Message *) IM);
  326.     }
  327.     D(("WaitPort..."));
  328.     WaitPort(Window->UserPort);
  329.     D(("WAKE!\n"));
  330.     }
  331. }
  332.